package com.example.demo.common.security;

import com.example.demo.common.exception.UserCountLockException;
import com.example.demo.entity.SysUser;
import com.example.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 自定义数据库认证
 */
@Service
public class MyUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws
            UsernameNotFoundException {

        SysUser sysUser = sysUserService.getByUsername(username);

        if (sysUser == null) {
            throw new UsernameNotFoundException("用户名或者密码错误！");
        } else if ("1".equals(sysUser.getStatus())) {
            throw new UserCountLockException("该用户账号已被封禁，具体联系管理员！");
        }

        return new User(sysUser.getUsername(), sysUser.getPassword(),
                getUserAuthority(sysUser.getId()));
    }

    public List<GrantedAuthority> getUserAuthority(Long userId) {
        // 格式
        // ROLE_admin,ROLE_common,system:user:resetPwd,system:role:delete,system:user:list,
        // system:menu:query,system:menu:list,system:menu:add,system:user:delete,system:rol
        // e:list,system:role:menu,system:user:edit,system:user:query,system:role:edit,syst
        // em:user:add,system:user:role,system:menu:delete,system:role:add,system:role:quer
        // y,system:menu:edit
        String authority = sysUserService.getUserAuthorityInfo(userId);
        System.out.println("authority=" + authority);
        return AuthorityUtils.commaSeparatedStringToAuthorityList(authority);
    }
}
